package common.mq.register.service.impl;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.jdbc.core.JdbcTemplate;

import common.mq.register.po.Consumer;
import common.mq.register.po.MessageBase;
import common.mq.register.service.IRegisterService;

public class RegisterServiceImpl implements IRegisterService {

	private List<Integer> uids = new ArrayList<Integer>();
	private Map<String, MessageBase> messageBases = new ConcurrentHashMap<String, MessageBase>(); // category-messageBase
	private Map<String, ArrayList<Consumer>> msgConsumers = new ConcurrentHashMap<String, ArrayList<Consumer>>(); // category-consumerList

	public RegisterServiceImpl(){
		MessageBase msgBase_test1 = new MessageBase();
		msgBase_test1.setCategory("testMsg1");
		Map<String, String> content_test1 = new HashMap<String,String>();
		content_test1.put("aInt", "int");
		content_test1.put("bString", "string");
		content_test1.put("cList", "list");
		content_test1.put("dBool", "bool");
		msgBase_test1.setContent(content_test1 );
		msgBase_test1.setOverdueTime(1000);
		messageBases.put("testMsg1", msgBase_test1 );
	}
	
	/**
	 * JdbcTemplate对象
	 */
	private JdbcTemplate jdbcTemplate;

	@Override
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	/**
	 * 判断该消息类型是否已注册
	 * 
	 * @param category
	 * @return
	 */
	@Override
	public boolean hasRegister(String category) {
		return messageBases.containsKey(category);
	}

	/**
	 * 根据消息类型查找元消息
	 * 
	 * @param category
	 * @return
	 */
	@Override
	public MessageBase getMessageBase(String category) {
		return messageBases.get(category);
	}

	/**
	 * 查找元消息对应的消费者列表（消息类型与元消息一一对应）
	 * 
	 * @param category
	 * @return
	 */
	@Override
	public ArrayList<Consumer> getConsumers(String category) {
		return msgConsumers.get(category);
	}

	@Override
	public boolean isUidExist(String uid) {
		return uids.contains(uid);
	}

}
